package cn.hahah.module.blog.app.service.Impl;

import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import cn.hahah.module.blog.app.dto.ArticleComment;
import cn.hahah.module.blog.app.dto.CommentReply;
import cn.hahah.module.blog.app.service.BlogVisitService;
import cn.hahah.module.blog.controller.admin.article.vo.ArticlePageReqVO;
import cn.hahah.module.blog.controller.admin.article.vo.ArticleRespVO;
import cn.hahah.module.blog.controller.admin.article.vo.ArticleSaveReqVO;
import cn.hahah.module.blog.controller.admin.userhistorybrowse.vo.UserHistoryBrowseSaveReqVO;
import cn.hahah.module.blog.controller.admin.userhistorysearch.vo.UserHistorySearchSaveReqVO;
import cn.hahah.module.blog.controller.admin.users.vo.UsersRespVO;
import cn.hahah.module.blog.controller.admin.visithistorybrowse.vo.VisitHistoryBrowseSaveReqVO;
import cn.hahah.module.blog.dal.dataobject.article.ArticleDO;
import cn.hahah.module.blog.dal.dataobject.articletag.ArticleTagDO;
import cn.hahah.module.blog.dal.dataobject.articletype.ArticleTypeDO;
import cn.hahah.module.blog.dal.dataobject.tag.TagDO;
import cn.hahah.module.blog.dal.dataobject.usercomment.UserCommentDO;
import cn.hahah.module.blog.dal.dataobject.userhistorybrowse.UserHistoryBrowseDO;
import cn.hahah.module.blog.dal.dataobject.userreply.UserReplyDO;
import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.dal.mysql.article.ArticleMapper;
import cn.hahah.module.blog.dal.mysql.articletag.ArticleTagMapper;
import cn.hahah.module.blog.dal.mysql.articletype.ArticleTypeMapper;
import cn.hahah.module.blog.dal.mysql.tag.TagMapper;
import cn.hahah.module.blog.dal.mysql.usercomment.UserCommentMapper;
import cn.hahah.module.blog.dal.mysql.userhistorybrowse.UserHistoryBrowseMapper;
import cn.hahah.module.blog.dal.mysql.userreply.UserReplyMapper;
import cn.hahah.module.blog.dal.mysql.users.UsersMapper;
import cn.hahah.module.blog.service.article.ArticleService;
import cn.hahah.module.blog.service.tag.TagService;
import cn.hahah.module.blog.service.userhistorybrowse.UserHistoryBrowseService;
import cn.hahah.module.blog.service.userhistorysearch.UserHistorySearchService;
import cn.hahah.module.blog.service.users.UsersService;
import cn.hahah.module.blog.service.visithistorybrowse.VisitHistoryBrowseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BlogVisitServiceImpl implements BlogVisitService {

    @Resource
    private ArticleService articleService;
    @Resource
    private TagMapper tagMapper;
    @Resource
    private ArticleTypeMapper articleTypeMapper;
    @Resource
    private UsersService usersService;
    @Resource
    private ArticleTagMapper articleTagMapper;
    @Resource
    private TagService tagService;
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private UsersMapper usersMapper;
    @Resource
    private VisitHistoryBrowseService visitHistoryBrowseService;
    @Resource
    private UserHistoryBrowseService userHistoryBrowseService;
    @Resource
    private UserHistorySearchService userHistorySearchService;
    @Resource
    private UserCommentMapper userCommentMapper;
    @Resource
    private UserReplyMapper userReplyMapper;
    @Resource
    private UserHistoryBrowseMapper userHistoryBrowseMapper;

    @Override
    public CommonResult<Map<String,Object>> homePageRecommend() {

        ArticlePageReqVO articlePageReqVO = new ArticlePageReqVO();
        articlePageReqVO.setPageNo(1);
        articlePageReqVO.setPageSize(10);
        PageResult<ArticleDO> pageResult = articleService.getArticlePage(articlePageReqVO);
        for (ArticleDO articleDO : pageResult.getList()) {
            articleDO.setContent("空");
        }

        List<TagDO> tagDOList = tagMapper.selectList();

        List<ArticleTypeDO> articleTypeDOList = articleTypeMapper.selectList();


        Map<String,Object> map = new HashMap<>();
        map.put("articlePage", pageResult);
        map.put("articleTag", tagDOList);
        map.put("articleType", articleTypeDOList);
        return CommonResult.success(map);
    }

    @Override
    public CommonResult<ArticleDO> articleDetail(Long userId, String ip, Long articleId) {
        if(userId == null){
            System.out.println("是游客"+ip);
            VisitHistoryBrowseSaveReqVO vo = new VisitHistoryBrowseSaveReqVO();
            vo.setVisit(ip);
            vo.setArticleId(articleId);
            visitHistoryBrowseService.createVisitHistoryBrowse(vo);
        }else {
            System.out.println("是用户"+userId);
            UserHistoryBrowseSaveReqVO vo = new UserHistoryBrowseSaveReqVO();
            vo.setUserId(userId);
            vo.setArticleId(articleId);
            userHistoryBrowseService.createUserHistoryBrowse(vo);
        }


        ArticleDO articleDO = articleService.getArticle(articleId);
        if(articleDO == null){
            CommonResult.error(400,"找不到文章");
        }
        List<UserHistoryBrowseDO> userHistoryBrowseDOList = userHistoryBrowseMapper.selectList(new LambdaQueryWrapper<UserHistoryBrowseDO>()
                .eq(UserHistoryBrowseDO::getArticleId, articleId));
        ArticleSaveReqVO vo = BeanUtils.toBean(articleDO, ArticleSaveReqVO.class);
        vo.setViews(userHistoryBrowseDOList.size());
        articleService.updateArticle(vo);

        return CommonResult.success(articleDO);
    }

    @Override
    public CommonResult<List<ArticleTypeDO>> getAllType(){
        List<ArticleTypeDO> articleTypeDOList = articleTypeMapper.selectList();
        return CommonResult.success(articleTypeDOList);
    }

    @Override
    public CommonResult<List<TagDO>> getAllTag(){
        List<TagDO> tagDOList = tagMapper.selectList();
        return CommonResult.success(tagDOList);
    }

    @Override
    public CommonResult<List<Long>> getTagByArt(Long articleId){
        List<ArticleTagDO> articleTagDOList = articleTagMapper.selectList(new LambdaQueryWrapper<ArticleTagDO>()
                .eq(ArticleTagDO::getArticleId, articleId));

        List<Long> tagDOList = new ArrayList<>();
        for (ArticleTagDO articleTagDO : articleTagDOList) {
            TagDO tagDO = tagService.getTag(articleTagDO.getTagId());
            tagDOList.add(tagDO.getId());
        }
        return CommonResult.success(tagDOList);
    }

    @Override
    public CommonResult<List<ArticleRespVO>> searchArticle(Long userId, String title) {
        //添加记录
        if(userId != null){
            UserHistorySearchSaveReqVO vo = new UserHistorySearchSaveReqVO();
            vo.setUserId(userId);
            vo.setContent(title);
            userHistorySearchService.createUserHistorySearch(vo);
        }


        List<ArticleDO> articleDOS = articleMapper.selectList(new LambdaQueryWrapper<ArticleDO>()
                .like(ArticleDO::getTitle, title));
        for (ArticleDO articleDO : articleDOS) {
            articleDO.setContent("空");
        }
        List<ArticleRespVO> list = BeanUtils.toBean(articleDOS, ArticleRespVO.class);


        return CommonResult.success(list);
    }

    @Override
    public CommonResult<List<UsersRespVO>> searchUser(String nickname) {

        List<UsersDO> usersDOList = usersMapper.selectList(new LambdaQueryWrapper<UsersDO>()
                .like(UsersDO::getNickname, nickname));
        for (UsersDO usersDO : usersDOList) {
            usersDO.setPassword("空");
        }
        List<UsersRespVO> usersRespVOS = BeanUtils.toBean(usersDOList, UsersRespVO.class);
        return CommonResult.success(usersRespVOS);
    }

    @Override
    public CommonResult<List<ArticleRespVO>> searchArtByType(Long typeId) {
        List<ArticleDO> articleDOList = articleMapper.selectList(new LambdaQueryWrapper<ArticleDO>()
                .eq(ArticleDO::getTypeId, typeId));

        for (ArticleDO articleDO : articleDOList) {
            articleDO.setContent("空");
        }

        List<ArticleRespVO> articleRespVOS = BeanUtils.toBean(articleDOList, ArticleRespVO.class);
        return CommonResult.success(articleRespVOS);
    }

    @Override
    public CommonResult<List<ArticleRespVO>> searchArtByTag(Long tagId) {
        List<ArticleTagDO> articleTagDOList = articleTagMapper.selectList(new LambdaQueryWrapper<ArticleTagDO>()
                .eq(ArticleTagDO::getTagId, tagId));

        List<ArticleDO> articleDOList = new ArrayList<>();
        for (ArticleTagDO articleTagDO : articleTagDOList) {
            ArticleDO articleDO = articleService.getArticle(articleTagDO.getArticleId());
            articleDO.setContent("空");
            articleDOList.add(articleDO);
        }

        List<ArticleRespVO> articleRespVOList =
                BeanUtils.toBean(articleDOList, ArticleRespVO.class);
        return CommonResult.success(articleRespVOList);
    }

    @Override
    public CommonResult<List<ArticleComment>> getArticleComment(Long articleId) {

        List<UserCommentDO> userCommentDOList = userCommentMapper.selectList(new LambdaQueryWrapper<UserCommentDO>()
                .eq(UserCommentDO::getArticleId, articleId));

        List<ArticleComment> articleCommentList = new ArrayList<>();
        for (UserCommentDO userCommentDO : userCommentDOList) {
            //获取数据
            ArticleComment articleComment = new ArticleComment();
            UsersDO usersDO = usersService.getUsers(userCommentDO.getUserId());
            usersDO.setPassword("空");
            List<UserReplyDO> userReplyDOList = userReplyMapper.selectList(new LambdaQueryWrapper<UserReplyDO>()
                    .eq(UserReplyDO::getCommentId, userCommentDO.getId()));
            List<CommentReply> commentReplyList = getReplyList(userReplyDOList);

            //设置数据
            articleComment.setId(userCommentDO.getId());
            articleComment.setUser(usersDO);
            articleComment.setContent(userCommentDO.getContent());
            articleComment.setReplies(commentReplyList);

            articleCommentList.add(articleComment);
        }

        return CommonResult.success(articleCommentList);
    }
    public List<CommentReply> getReplyList (List<UserReplyDO> userReplyDOList) {

        List<CommentReply> commentReplyList = new ArrayList<>();

        for (UserReplyDO userReplyDO : userReplyDOList) {
            CommentReply commentReply = new CommentReply();
            UsersDO usersDO = usersService.getUsers(userReplyDO.getUserId());
            usersDO.setPassword("空");

            commentReply.setId(userReplyDO.getId());
            commentReply.setCommentId(userReplyDO.getCommentId());
            commentReply.setUser(usersDO);
            commentReply.setContent(userReplyDO.getContent());

            commentReplyList.add(commentReply);
        }
        return commentReplyList;
    }

    @Override
    public CommonResult<UsersDO> checkUser(Long userId) {
        UsersDO usersDO = usersService.getUsers(userId);
        usersDO.setPassword(null);
        return CommonResult.success(usersDO);
    }
}
